package com.zz.push.client;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PushClientChannelInitializer extends ChannelInitializer<SocketChannel> {

    private static Logger logger = LoggerFactory.getLogger(PushClientChannelInitializer.class);

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast("idle", new IdleStateHandler(60, 0, 0));

//        pipeline.addFirst(new ChannelInboundHandlerAdapter() {
//            @Override
//            public void channelInactive(ChannelHandlerContext ctx) throws Exception {
//                super.channelInactive(ctx);
//                logger.info("handler:Reconnect");
//                final EventLoopGroup loop = ctx.channel().eventLoop();
//                loop.schedule(() -> client.connect(new Bootstrap(), loop), 10L, TimeUnit.SECONDS);
//            }
//        });

        pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast("handler", new PushClientHandler());

    }
}
